package ua.privatbank.test;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;
import org.junit.*;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;

import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.security.UserAndPassword;
import org.openqa.selenium.support.ui.Select;

@RunWith(Parameterized.class)
public class EnterToP24 {

    private WebDriver driver;
    private String baseUrl;
    private UserAndPassword user;
    private boolean acceptNextAlert = true;
    private final StringBuffer verificationErrors = new StringBuffer();

    public EnterToP24(WebDriver driver, String baseUrl, UserAndPassword user) {
        this.driver = driver;
        this.baseUrl = baseUrl;
        this.user = user;
    }

    @Parameterized.Parameters
    public static Collection<Object[]> init() {
        return Arrays.asList(new Object[][] {
                { new FirefoxDriver(), "http://localhost:8282/", new UserAndPassword("937103721", "q018bfe912b8c") }//,
                //{ new ChromeDriver(), "http://localhost:8282/", new UserAndPassword("937103721", "q018bfe912b8c") }
        });
    }

    @Before
    public void setUp() throws Exception {
        driver.manage().timeouts()
                .pageLoadTimeout(15, TimeUnit.SECONDS)
                .setScriptTimeout(5, TimeUnit.SECONDS)
                .implicitlyWait(10, TimeUnit.SECONDS);
    }

    @Test
    public void testEnterToP24() throws Exception {
        driver.get(baseUrl + "p24/login");
        driver.findElement(By.id("UserName")).sendKeys(user.getUsername());
        driver.findElement(By.id("UserPass")).clear();
        driver.findElement(By.id("UserPass")).sendKeys(user.getPassword());
        driver.findElement(By.className("bt_login")).click();

        /*for(WebElement webElement: driver.findElements(By.className("otp-item-field"))) {
            webElement.clear();
            switch (Integer.parseInt(webElement.getAttribute("item"))) {
                case 0: webElement.sendKeys("95"); break;
                case 1: webElement.sendKeys("40"); break;
                case 2: webElement.sendKeys("50"); break;
                case 3: webElement.sendKeys("19"); break;
            }
        }*/

        while(!isFieldsDone(2)) {

        }
        driver.findElement(By.className("bt_login")).click();
        if(!driver.getCurrentUrl().contains("/news")) {
            throw new Exception("Не удалось попасть в сессию!");
        }
    }

    @After
    public void tearDown() throws Exception {
        driver.quit();
        String verificationErrorString = verificationErrors.toString();
        if (!"".equals(verificationErrorString)) {
            fail(verificationErrorString);
        }
    }

    private boolean isElementPresent(By by) {
        try {
            driver.findElement(by);
            return true;
        } catch (NoSuchElementException e) {
            return false;
        }
    }

    private String closeAlertAndGetItsText() {
        try {
            Alert alert = driver.switchTo().alert();
            if (acceptNextAlert) {
                alert.accept();
            } else {
                alert.dismiss();
            }
            return alert.getText();
        } finally {
            acceptNextAlert = true;
        }
    }

    private boolean isFieldsDone(int length) throws InterruptedException {
        boolean result = true;
        for(WebElement element: driver.findElements(By.className("otp-item-field"))) {
            result &= (element.getText().length()==length);
            System.out.printf("[%s]: Text:%s Length:%d\n",
                    element.getAttribute("item"), element.getText(), element.getText().length());
        }
        Thread.sleep(2000);
        return result;
    }
}
